/*
 * This file is part of LexiToolsMobile
 *  
 * Author: Jerzy Wawro
 * ver. 1.00
 * Copyright: Tenar (http://www.tenar.pl)
 *
 * Mobile LexiTools is an open source project; you can redistribute it 
 * and/or modify it under the terms of the license as published -  GPL with 
 * the following modification:  fragments of the code may be not copyleft 
 * (hidden), to protect contents of the dicionaries.
 * In this case the author of non-public fragments of the code is obliged 
 * to make available functionally equivalent code for unprotected dictionaries.
 * 
 * Mobile LexiTools is distributed WITHOUT ANY WARRANTY.
 * See the license for more details.
 *  
 * You should have received a copy of the GNU General Public License
 * along with LexiToolsMobile (see: gpl.txt); if not - you can find it 
 * on web page http://www.fsf.org/, or write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package dict;

import java.io.*;
import java.util.*;
import javax.microedition.io.*;
//#if FileConnection=="1"
//# import javax.microedition.io.file.*;
//#endif

public class pgfile {

static int PAGES_PER_FILE = 32;
static int PAGESIZE = 512;
static int FILESIZE = PAGESIZE * PAGES_PER_FILE;
static int SRC_RESOURCE = 0;
static int SRC_FS = 1;

static String HomeDir;
    
String filename;
String uri;

//#if FileConnection=="1"
//# FileConnection file;
//#endif
InputStream fis;
int iSource;
/*======================= FUNKCJE ========================*/

public pgfile(int source, String fn, String root) {
 filename=fn;
 iSource=source;
 if (iSource==SRC_RESOURCE)  uri="/"; 
 else uri=root+"/";
}

boolean open(int n)
//! [zaimplementowa� jako metode wirtualna i uzupelnic w dict0]    
{
 if (iSource==SRC_RESOURCE) {
   try {
     if (filename.startsWith("pl") || filename.startsWith("en")) {
       fis = getClass().getResourceAsStream(uri+filename+'.'+String.valueOf(n));
     } else  {
       // do poprawienia - ujednolicenia
       fis = getClass().getResourceAsStream(uri+filename+'.'+String.valueOf(n-1));
     }
     if (fis==null) return (false);
     else return (true);
    } catch (Exception e) {
         throw new Error("Failed to read file" + e);
    }
 } else {
// lokalny system plikow
//#if FileConnection=="1"     
//#    try 
//#    {
//# //   fis = this.getClass().getResourcesAsStream(filename+'.'+IntToStr(n));
//#      if (filename.compareTo("tas")==0) {
//#        // do poprawienia - ujednolicenia
//#        file = (FileConnection)Connector.open(uri+filename+'.'+String.valueOf(n-1));
//#      }
//#      else {
//#        file = (FileConnection)Connector.open(uri+filename+'.'+String.valueOf(n));
//#      }
//#      fis = file.openInputStream();
//# 
//#      return (true);
//#    } catch (Exception e) {
//#     System.err.println("Error: " + e);
//#    }   
//#endif   
 }     
 return (false);
}  /* open */

void close()
{
 try {
     if (fis != null) {
        fis.close();  
     }
 } catch (IOException e) {
    System.err.println("error: " + e);
 }
} /* pgclose */


void read(int n, page[] pages, int ix)
{
    int i,rs;
    
    if (open(n)) {
      try  {
          rs=PAGESIZE;
          for (i=0; (i<PAGES_PER_FILE) && (rs==PAGESIZE); i++) {
              rs=fis.read(pages[ix+i].buffer, 0, PAGESIZE);
              if ((rs<PAGESIZE) && (i<PAGES_PER_FILE-1)) {
                     System.err.println("error - pagesize "); 
              }
          }
       }
      catch (IOException e) {
        System.err.println("error: " + e);
      }
    }
    close();
} /* read */


}
